package com.adminserver.util;

import com.adminserver.customEntity.UnionUserRoleEntity;
import com.adminserver.entity.PermissionEntity;
import com.adminserver.entity.UserPermissionEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

@Component
public class FormatUserRoleData {

    /**
     * 格式化用户权限数据
     * @param maps
     * @return
     */
    public static HashSet<UnionUserRoleEntity> formatUserRole(List<Map<String, Object>> maps) {
        HashSet<UnionUserRoleEntity> result = new HashSet<>();

        for (Map<String, Object> map : maps) {
            UnionUserRoleEntity unionUserRoleEntity = new UnionUserRoleEntity();
            unionUserRoleEntity.setUserId(map.get("user_id").toString());
            unionUserRoleEntity.setRoleId(map.get("role_id").toString());
            unionUserRoleEntity.setRoleName(map.get("role_name").toString());
            unionUserRoleEntity.setEnabled((Boolean) map.get("enabled"));
            unionUserRoleEntity.setUserNickname(map.get("user_nickname"));
            unionUserRoleEntity.setWorkerName(map.get("worker_name"));
            unionUserRoleEntity.setName(map.get("name"));
            result.add(unionUserRoleEntity);
        }

        for (UnionUserRoleEntity entity : result) {
            ArrayList<PermissionEntity> permissionEntities = new ArrayList<>();
            for (Map<String, Object> map : maps) {
                if (entity.getUserId().equals(map.get("user_id").toString())) {
                    PermissionEntity permissionEntity = new PermissionEntity();
                    permissionEntity.setPermId(map.get("perm_id").toString());
                    permissionEntity.setPermName(map.get("perm_name").toString());
                    permissionEntities.add(permissionEntity);
                }
            }
            entity.setPermissionEntities(permissionEntities);
        }
        return result;
    }

    /**
     * 检查限制权限，并更新 用户权限数据
     * @param list 目标权限数据
     * @param hashSet 源权限数据
     * @return 更新权限数据 HashSet<UnionUserRoleEntity>
     */
    public static HashSet<UnionUserRoleEntity> checkLimitPermission(List<UserPermissionEntity> list, HashSet<UnionUserRoleEntity> hashSet) {

        for (UnionUserRoleEntity userRole : hashSet) {
            ArrayList<PermissionEntity> permissionEntityArrayList = new ArrayList<>();
            for (UserPermissionEntity userPermission : list) {
                if (userRole.getUserId().equals(userPermission.getUserId())) {
                    PermissionEntity permissionEntity = new PermissionEntity();
                    permissionEntity.setPermId(userPermission.getPermissionId());
                    permissionEntity.setPermName(userPermission.getPermName());
                    permissionEntityArrayList.add(permissionEntity);
                }
            }
            if (permissionEntityArrayList.size() != 0){
                userRole.setPermissionEntities(permissionEntityArrayList);
            }
        }

        return hashSet;
    }
}
